package com.lmj.mybatisPlus.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lmj.mybatisPlus.constant.EmployeeEnumType;
import com.lmj.mybatisPlus.entry.po.Employee;
import com.lmj.mybatisPlus.mapper.EmployeeMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @classDesc: 功能描述:这是po不是vo，不推荐这样用，只是实验转换为json的效果
 * @author: 李明杰
 * @date: 2021/11/13 0:02
 * @copyright
 */
@RestController
public class EmployeeController {
    @Resource
    EmployeeMapper employeeMapper;


    @GetMapping("/toSelect")
    public List<Employee> toSelect() throws JsonProcessingException {
        List<Employee> employees = employeeMapper.selectList(null);
        /**
         * 关于枚举转换成json显示问题：
         * 1.使用@RestController注解或者Jackson转换成json都可以解决枚举的值
         * 2.因为给前台返回的json，前台枚举变成值了。后台java枚举没有变化
         */
//        ObjectMapper mapper = new ObjectMapper();
//        String json = mapper.writeValueAsString(employees);
//        System.err.println(json);
//        System.err.println(employees);
        return employees;
    }

    @GetMapping("/toInsert")
    public int toInsert() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, String> contact = new HashMap<>();
        contact.put("phone2", "010-884548");
        contact.put("tel2", "13****9999");

        Employee employee = new Employee(null, "toInsert", "toInsert@qq", EmployeeEnumType.women, 1, contact);
        int insert = employeeMapper.insert(employee);
        return insert;
    }

}
